package dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mysql.jdbc.PreparedStatement;

import dao.UserDao;
import model.Announces;
import model.User;
import util.JDBCUtiles;

public class UserDaoImpl implements UserDao {

	@Override
	public List<User> getAllUser() throws SQLException {
		// TODO Auto-generated method stub
		//得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		//sql语句
		String sql = "select * from users" ;
		return qr.query(sql, new BeanListHandler<User>(User.class));		
	}

	@Override
	public User getUserByIdAndPwd(String id, String password) throws SQLException {
		// TODO Auto-generated method stub
		//得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		//sql语句
		String sql = "SELECT * FROM users WHERE UserID=? AND UserPwd=?" ;
		return qr.query(sql, new BeanHandler<User>(User.class),id,password);
	}

	@Override
	public User getUserById(String id) throws SQLException {
		// TODO Auto-generated method stub
		//得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		//sql语句
		String sql = "SELECT * FROM users WHERE UserID=?" ;
		return qr.query(sql, new BeanHandler<User>(User.class),id);
	}

	@Override
	public boolean register(String id, String name, String sex, String pwd1) throws SQLException {
		// TODO Auto-generated method stub
				//得到QueryRunner对象
				QueryRunner qr = JDBCUtiles.getQueryRunner();
				//sql语句
//				String sql  = "insert into users "
//						+ "values(?,?,?,?,?,?,?,?,?,?)";
				String sql = "INSERT INTO users (UserID,UserName,UserSex,UserPwd,UserTime) VALUES (?,?,?,?,?)";
//				int i = qr.update(sql, user.getUserId(),user.getUserName(),
//						user.getUserPwd(),user.getUserSex(),user.getUserBirth(),
//						user.getUserAnnCount(),user.getUserShow(),user.getUserTime(),
//						user.getUserPhone(),user.getUserEmail());
				Date date = new Date();
				int i = qr.update(sql,id,name,sex,pwd1,date);
				return  i > 0 ? true : false;
	}

	@Override
	public List<User> getUserListByAnn(List<Announces> annList) throws SQLException {
		// TODO Auto-generated method stub
		List<User> userList = new ArrayList<>(); 
		for(int i = 0 ; i < annList.size() ; i++) {
			Announces ann = annList.get(i);
			userList.add(this.getUserById(ann.getUserID()));
		}
		return userList;
	}

	@Override
	public boolean register(User user) throws SQLException {
		// TODO Auto-generated method stub
		//得到QueryRunner对象
		QueryRunner qr = JDBCUtiles.getQueryRunner();
		//sql语句
		String sql  = "insert into users "
				+ "values(?,?,?,?,?,?,?,?,?,?)";
		int i = qr.update(sql, user.getUserId(),user.getUserName(),
				user.getUserPwd(),user.getUserSex(),user.getUserBirth(),
				user.getUserAnnCount(),user.getUserShow(),user.getUserTime(),
				user.getUserPhone(),user.getUserEmail());
		return  i > 0 ? true : false;
	}
	


}
